//
//  DatePickerView.m
//  benben
//
//  Created by wlm on 15-4-17.
//  Copyright (c) 2015年 xunao. All rights reserved.
//

#import "DatePickerView.h"

@interface DatePickerView()
{
    UIDatePicker *datePicker;
}

@end

@implementation DatePickerView

+(DatePickerView *)datePicker
{
    return [[DatePickerView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
}

-(void)showPicker
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    if (!keyWindow) {
        keyWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    if (![keyWindow.subviews containsObject:self]) {
        [keyWindow addSubview:self];
        
        CGRect startFrame = datePicker.frame;
        CGRect endFrame = startFrame;
        startFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
        datePicker.frame = startFrame;
        [UIView animateWithDuration:0.3 animations:^{
            
            datePicker.frame = endFrame;
            self.backgroundColor = kRGBACOLOR(51, 51, 51, 0.3);
        }];
    }
}

-(void)dismiss{
    
    [self removeFromSuperview];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        datePicker =  [[UIDatePicker alloc] initWithFrame:CGRectMake(0, frame.size.height-255, [UIScreen mainScreen].bounds.size.width, 210)];
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        [datePicker setDate:[NSDate date]];
        [datePicker setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:datePicker];
        
        UIView *btnBg = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height-50, [UIScreen mainScreen].bounds.size.width, 50)];
        [btnBg setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
        [self addSubview:btnBg];
        
        UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, frame.size.height - 50, ([UIScreen mainScreen].bounds.size.width-30)/2.0, 45)];
        [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(cancelPicker) forControlEvents:UIControlEventTouchUpInside];
//        [leftBtn setTag:201];
        [leftBtn.layer setCornerRadius:3];
        [leftBtn setBackgroundColor:[UIColor colorWithWhite:168.0/255 alpha:1]];
        [leftBtn.titleLabel setTextColor:[UIColor whiteColor]];
        [leftBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [self addSubview:leftBtn];
        
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-30)/2.0+20, frame.size.height - 50,   ([UIScreen mainScreen].bounds.size.width-30)/2.0, 45)];
        [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
//        [rightBtn setTag:200];
        [rightBtn.layer setCornerRadius:3];
        [rightBtn setBackgroundColor:[UIColor colorWithRed:0.99f green:0.26f blue:0.22f alpha:1.00f]];
        [rightBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
        [rightBtn.titleLabel setTextColor:[UIColor whiteColor]];
        [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [self addSubview:rightBtn];
        
    }
    return self;
}

-(void)setDatePickerMode:(UIDatePickerMode)datePickerMode
{
    [datePicker setDatePickerMode:datePickerMode];
}

-(void)setDate:(NSDate *)date
{
    if (date) {
        [datePicker setDate:date];
    }else {
        [datePicker setDate:[NSDate date]];
    }
}

-(void)submit
{
    [self removeFromSuperview];
    if (self.completion) {
        self.completion([datePicker date]);
    }
}

-(void)cancelPicker
{
    [self removeFromSuperview];
    if (self.canceled) {
        self.canceled();
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    if (touchPoint.y < self.frame.size.height-datePicker.frame.size.height) {
        [self cancelPicker];
    }
}

@end
